package util

import (
	"nspel/entity"
	"nspel/nspel"
	"strconv"
)

func GetSchoolsListAdministrator(provinceId int64, cityId int64, areaId int64, schoolName string, joinYear int64, pageSize int64, currentPage int64) (count int64, schools []entity.School, err error) {
	school := new(entity.School)
	ormEng := nspel.GetOrmEngine().QueryTable(school.TableName())
	hashKey := ""
	if provinceId != 0 {
		ormEng = ormEng.Filter("province_id", provinceId)
		hashKey += "provinceId[" + strconv.FormatInt(provinceId, 10) + "]."
	}
	if cityId != 0 {
		ormEng = ormEng.Filter("city_id", cityId)
		hashKey += "cityId[" + strconv.FormatInt(cityId, 10) + "]."
	}
	if areaId != 0 {
		ormEng = ormEng.Filter("experiment_area_id", areaId)
		hashKey += "experimentAreaId[" + strconv.FormatInt(areaId, 10) + "]."
	}
	ormEng = ormEng.Filter("name__contains", schoolName)
	hashKey += "name[\"" + schoolName + "\"]."
	if joinYear != 0 {
		if joinYear == -1 {
			ormEng = ormEng.Filter("join_at__isnull", true)
			hashKey += "joinAt[null]."
		} else {
			ormEng = ormEng.Filter("join_at__gte", strconv.FormatInt(joinYear, 10)+"-01-01")
			ormEng = ormEng.Filter("join_at__lte", strconv.FormatInt(joinYear, 10)+"-12-31")
			hashKey += "joinAt[" + strconv.FormatInt(joinYear, 10) + "]."
		}
	}
	count, err = school.SelectCountByProvinceIdCityIdExperimentAreaIdNameJoinAt(&ormEng, hashKey+"count")
	if err != nil {
		return
	}
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	schools, err = school.SelectAllByProvinceIdCityIdExperimentAreaIdAreaIdNameJoinYear(&ormEng, hashKey)
	return
}

func GetSchoolsListArea(areaId int64, joinYear int64, schoolId int64, pageSize int64, currentPage int64) (count int64, schools []entity.School, err error) {
	school := new(entity.School)
	ormEng := nspel.GetOrmEngine().QueryTable(school.TableName())
	hashKey := "experimentAreaId[" + strconv.FormatInt(areaId, 10) + "]."
	ormEng = ormEng.Filter("experiment_area_id", areaId)
	if joinYear != 0 {
		if joinYear == -1 {
			ormEng = ormEng.Filter("join_at__isnull", true)
			hashKey += "joinAt[null]."
		} else {
			ormEng = ormEng.Filter("join_at__gte", strconv.FormatInt(joinYear, 10)+"-01-01")
			ormEng = ormEng.Filter("join_at__lte", strconv.FormatInt(joinYear, 10)+"-12-31")
			hashKey += "joinAt[" + strconv.FormatInt(joinYear, 10) + "]."
		}
	}
	if schoolId != 0 {
		ormEng = ormEng.Filter("id", schoolId)
		hashKey += "id[" + strconv.FormatInt(schoolId, 10) + "]."
	}
	count, err = school.SelectCountByProvinceIdCityIdExperimentAreaIdNameJoinAt(&ormEng, hashKey+"count")
	if err != nil {
		return
	}
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	schools, err = school.SelectAllByProvinceIdCityIdExperimentAreaIdAreaIdNameJoinYear(&ormEng, hashKey)
	return
}

func AddMapFloat2(beSet *map[string][2]float64, key string, index int, value float64) {
	temp := (*beSet)[key]
	temp[index] += value
	(*beSet)[key] = temp
}

func AddMapInt644(beSet *map[string][4]int64, key string, index int, value int64) {
	temp := (*beSet)[key]
	temp[index] += value
	(*beSet)[key] = temp
}
